package com.jcsz.gulimall.member.service.impl;

import com.jcsz.common.vo.MemberLoginVo;
import com.jcsz.common.vo.MemberRegistVo;
import com.jcsz.gulimall.member.entity.MemberLevelEntity;
import com.jcsz.gulimall.member.exception.PhoneExsitException;
import com.jcsz.gulimall.member.exception.UserNameExsitException;
import com.jcsz.gulimall.member.service.MemberLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jcsz.common.utils.PageUtils;
import com.jcsz.common.utils.Query;

import com.jcsz.gulimall.member.dao.MemberDao;
import com.jcsz.gulimall.member.entity.MemberEntity;
import com.jcsz.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo memberRegistVo) {
        MemberEntity memberEntity = new MemberEntity();
        // 1、拿到用户默认会员等级
        MemberLevelEntity defaultMember = memberLevelService.getDefaultLevel();
        memberEntity.setLevelId(defaultMember.getId());
        // 2、拿到用户名和手机号
        String userName = memberRegistVo.getUserName();
        String phone = memberRegistVo.getPhone();
        // 3、并验证是否唯一,不唯一抛出异常
        checkUserNameUnique(userName);
        checkPhoneUnique(phone);
        // 4、设置相关属性
        memberEntity.setMobile(phone);
        memberEntity.setUsername(userName);
        // 5、密码进行加密，防止数据库密码泄露后被别人登录
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(memberRegistVo.getPassword());
        memberEntity.setPassword(encode);
        // 6、保存
        save(memberEntity);
    }

    @Override
    public MemberEntity login(MemberLoginVo memberLoginVo) {
        // 1、根据用户名和手机号查询是否拥有该用户
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", memberLoginVo.getLoginacct()).or().eq("mobile", memberLoginVo.getLoginacct()));
        // 2、没有该用户返回null
        if (memberEntity == null) {
            return null;
        } else {
            // 3、拿到用户密码进行解密，验证是否相等
            String passwordDB = memberEntity.getPassword();
            // 4、和用户密码进行校验
            boolean matcheResult = new BCryptPasswordEncoder().matches(memberLoginVo.getPassword(), passwordDB);
            if (matcheResult) {
                // 5、密码验证成功
                return memberEntity;
            } else {
                // 6、密码不相等返回null
                return null;
            }
        }
    }

    private void checkPhoneUnique(String phone) throws PhoneExsitException {
        Integer selectCount = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (selectCount > 0) {
            // 没有就抛出异常
            throw new PhoneExsitException();
        }
    }

    private void checkUserNameUnique(String userName) throws UserNameExsitException {
        Integer selectCount = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (selectCount > 0) {
            // 没有就抛出异常
            throw new UserNameExsitException();
        }
    }

}